﻿/// <reference path="../scripts/jquery-1.11.2.min.js" />
/// <reference path="../scripts/bootstrap.min.js" />

if (typeof ($) != "undefined" && $) {
    //验证用户
    CheckUser();
    $(document).ready(onReady);
}

function onReady() {
    var ua = navigator.userAgent.toLowerCase();
    var isIos = (ua.indexOf('iphone') != -1) || (ua.indexOf('ipad') != -1);
    $("#mapcenter").height($(window.document).height() - 90);
    getWait();
    $.ajax({
        type: "get",
        url: "api/member.ashx?ac=carList&k=" + Math.random(),
        beforeSend: function () {
            $('#loadingToast').show();
        },
        success: function (data) {
            var list = eval("(" + data + ")");
            if (list.length > 0) {
                var o = list[0];
                var htm = o.CarType + ' [ ' + o.CarNumber + ' ]';
                $("#car").html(htm);
                $("#carid").val(o.Id);
                $("#cartype").val(o.CarType);
                $("#carnumber").val(o.CarNumber);
            }
            for (var i = 0; i < list.length; i++) {
                var o = list[i];
                var htm = '<div class="weui_cells weui_cells_access">';
                htm += '<div class="weui_cell" onclick="setcar(' + o.Id + ',\'' + o.CarType + '\',\'' + o.CarNumber + '\')">';
                htm += '<div class="weui_cell_hd">';
                htm += '<img alt="logo" src="' + o.CarLogo + '" height="68px" width="68px" />';
                htm += '</div>';
                htm += '<div class="weui_cell_bd weui_cell_primary pl20">';
                htm += '车牌号：' + o.CarNumber + '<br />';
                htm += '车型：' + o.CarType + '<br />';
                htm += '颜色：' + o.Color + '<br />';
                htm += '</div>';
                htm += '<div class="weui_cell_ft"></div>';
                htm += '</div>';
                htm += '</div>';

                $("#mycar").append(htm);
            }
        },
        complete: function () {
            $('#loadingToast').hide();
        }
    });

    //定位
    get_location();

    //加载服务价格
    $.ajax({
        type: "get",
        url: "api/common.ashx?ac=project&k=" + Math.random(),
        success: function (data) {
            var list = eval("(" + data + ")");
            for (var i = 0; i < list.length; i++) {
                var o = list[i];
                if (o.Project == '外观') {
                    $("#outprice").html(toDecimal2(o.Pirce));
                    $("#outtime").val(o.Times);
                }
                else if (o.Project == '内饰') {
                    $("#inprice").html(toDecimal2(o.Pirce));
                    $("#intime").val(o.Times);
                }
            }
            setMoney();
        }
    });
    //加载账户余额
    $.ajax({
        type: "get",
        url: "api/member.ashx?ac=account&k=" + Math.random(),
        success: function (data) {
            if (data != null && data != '') {
                var m = eval("(" + data + ")");
                $("#account").html(toDecimal2(m.account));
                if (($("#cptype").val() == 0) && Number(m.account) > 0) {
                    $("#labacc").removeAttr('style');
                }
            }
        }
    });
    $("#divcar").click(function () {
        $("#carlist").show();
        $("#orderinfo").hide();
    });
    $("#divadd").click(function () {
        $("#menu", window.parent.document).children("div").removeClass("active");
        $("#car", window.parent.document).addClass("active");
        window.location.href = "car.html";
    });

    $("#divlocation").click(function () {
        showmap();
    });

    $("#chkin").change(function () {
        setMoney();
    });

    $("#chkacc").change(function () {
        if ($(this).is(':checked') == true) {
            var ac = Number($("#account").html());
            var pay = Number($("#money").html());
            if (ac > 0 && pay > 0) {
                $("#txtacc").html(toDecimal2(ac > pay ? pay : ac));
            }
            $("#spanacc").removeClass('none');
        }
        else {
            $("#txtacc").html('0.00');
            $("#spanacc").addClass('none');
        }
        setMoney();
    });

    //加载洗车券
    $.ajax({
        type: "get",
        url: "api/member.ashx?ac=coupons&t=9&u=1&od=aes&k=" + Math.random(),
        success: function (data) {
            var list = eval("(" + data + ")");
            for (var i = 0; i < list.length; i++) {
                var o = list[i];
                if (i == 0) {
                    setCoupons(o.Id, o.CouType, o.Money);
                }
                if (o.CouType == '0') {
                    $("#mycp").append('<div class="stamp ' + o.Css + '" onclick="setCoupons(' + o.Id + ',' + o.CouType + ',' + o.Money + ')"><div class="par"><sub class="sign">￥</sub><span>' + o.Money + '.00</span><sub></sub><p>' + o.Explain + '</p></div><div class="copy">有效期<p>' + moment(o.YouXQStart).format('YYYY-MM-DD') + '<br />' + moment(o.YouXQEnd).format('YYYY-MM-DD') + '<br />' + o.Tip + '</p><i></i></div></div>');
                }
                else {
                    $("#mycp").append('<div class="stamp ' + o.Css + '" onclick="setCoupons(' + o.Id + ',' + o.CouType + ',' + o.Money + ')"><div class="par"><sub class="sign"></sub><span>洗车券</span><sub></sub><p>' + o.Explain + '</p></div><div class="copy">有效期<p>' + moment(o.YouXQStart).format('YYYY-MM-DD') + '<br />' + moment(o.YouXQEnd).format('YYYY-MM-DD') + '<br />' + o.Tip + '</p><i></i></div></div>');
                }
            }
        }
    });
    //选择洗车券
    $("#divcoupons").click(function () {
        $("#cplist").show();
        $("#orderinfo").hide();
    });
    //购买洗车券
    $("#btnbuy").click(function () {
        window.location.href = 'cpbuy.html';
    });

    //提交
    $("#btnSubmit").click(function () {
        var carid = $("#carid").val();
        var carnmber = $("#carnumber").val();
        var cartype = $("#cartype").val();

        var address = $("#address").html();
        var lat = $("#lat").val();
        var lng = $("#lng").val();

        var inter = $("#chkin").is(':checked') == true ? 1 : 0;
        var price = $("#price").val();
        var times = $("#times").val();

        var cpid = $("#cpid").val();
        var cptype = $("#cptype").val();
        var cpmoney = $("#cpmoney").val();
        var cash = $("#txtacc").html();
        var paymoney = $("#money").html();
        var memo = $("#memo").val();

        if (carid == '0' || carid == '') {
            showToast('请选择服务车辆');
        }
        else if (address == '' || lat == '0' || lng == '0') {
            showToast('请设置车辆位置');
        }
        else {
            var data = { carid: carid, carnmber: carnmber, cartype: cartype, address: address, lat: lat, lng: lng, inter: inter, price: price, times: times, cpid: cpid, cptype: cptype, cpmoney: cpmoney, cash: cash, paymoney: paymoney, memo: memo };
            $.ajax({
                type: "post",
                data: data,
                url: "api/order.ashx?ac=submit&k=" + Math.random(),
                beforeSend: function () {
                    $('#loadingToast').show();
                },
                error: function () {
                    showToast("订单提交失败");
                },
                success: function (data) {
                    var obj = eval("(" + data + ")").result;
                    if (obj.r == "suc") {
                        if (obj.val == '0') {
                            window.location.href = "list.html";
                        }
                        else {
                            window.parent.location.href = "JsApiPayPage.aspx?OrderID=" + obj.val;
                        }
                    }
                    else {
                        showToast(obj.val);
                    }
                },
                complete: function () {
                    $('#loadingToast').hide();
                }
            });
        }
    });
}

function setcar(id, type, num) {
    $("#car").html(type + ' [ ' + num + ' ] ');
    $("#carid").val(id);
    $("#cartype").val(type);
    $("#carnumber").val(num);

    $("#carlist").hide();
    $("#orderinfo").show();
}

function cpback() {
    $("#cplist").hide();
    $("#orderinfo").show();
}

function setCoupons(id, cptype, money) {
    $("#cpid").val(id);
    $("#cptype").val(cptype);
    if (cptype == '0') {
        $("#cp").html('￥' + toDecimal2(money));
        $("#cpmoney").val(money);
    }
    else {
        $("#cp").html('洗车券×1');
        $("#labacc").attr({ style: "display:none" });
    }
    $("#chkacc").removeAttr('checked');
    $("#txtacc").html('0.00');
    $("#spanacc").addClass('none');
    setMoney();
    $("#cplist").hide();
    $("#orderinfo").show();
}

function setMoney() {
    var op = Number($("#outprice").html());
    var ot = Number($("#outtime").val());
    var ip = Number($("#interprice").html());
    var it = Number($("#intime").val());
    var ac = Number($("#txtacc").html());

    var cp = Number($("#cpmoney").val());
    var cptype = $("#cptype").val();
    var p = op;
    var t = ot;
    if ($("#chkin").is(':checked') == true) {
        p = p + ip;
        t = t + it;
    }
    if (p > 0) {
        $("#price").val(p);
        $("#times").val(t);

        if (cptype == '0') {
            var m = p - cp - ac;
            m = m < 0 ? "0.00" : toDecimal2(m);
            $("#money").html(m);
            $("#pay").html('￥' + m);
        }
        else {
            $("#money").html("0.00");
            $("#pay").html('￥0.00');
        }
        $("#btnSubmit").removeAttr('disabled');
    }
}
function getWait() {
    $.ajax({
        type: "get",
        url: "api/order.ashx?ac=getWait&k=" + Math.random(),
        success: function (data) {
            var obj = eval("(" + data + ")");
            $("#waitCount").html(obj.count);
            $("#waitTime").html(obj.time);
            $("#time").html(obj.time);
            if (obj.show == 1) {
                $('#dialog1').show();
                $('#dialog1').find('.weui_btn_dialog').on('click', function () {
                    $('#dialog1').hide();
                });
            }
        }
    });
}

